package com.my.control;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.my.bean.Part;
import com.my.util.PageBean;

public class PartAction extends BaseAction {

	// 此属性对应于表单中文件字段的名称
	private File uploadFile;
	// 下面的这两个属性的命名必须遵守上定的规则，即为"表单中文件字段的名称" + "相应的后缀"
	private String uploadFileContentType; // 得到上传的文件的数据类型,
	private String uploadFileFileName; // 得到上传的文件的名称

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public String addPart() throws Exception {
		if (uploadFile == null) {
			request.put("message", "请选择正确的图片!");
			return "addpart";
		}
		String realPath = "D:/workspace/huashuoshouhou/WebContent/images";
		SimpleDateFormat date = new SimpleDateFormat("/yyyy/MM/dd");
		String dateTime = date.format(new Date());
		realPath += dateTime;

		uploadFileFileName = UUID.randomUUID().toString()
				+ uploadFileFileName.substring(uploadFileFileName
						.lastIndexOf('.'));

		System.out.println(uploadFileContentType);
		// 控制图片类型
		if (uploadFileContentType.equals("image/gif")
				|| uploadFileContentType.equals("image/jpeg")
				|| uploadFileContentType.equals("image/png")
				|| uploadFileContentType.equals("image/bmp")
				|| uploadFileContentType.equals("image/x-icon")
				|| uploadFileContentType.equals("image/pjpeg")) {
			// 判断文件是否为空,并且文件不能大于2M
			if (uploadFile != null && uploadFile.length() < 2097152) {
				// 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
				File filePath = new File(new File(realPath), uploadFileFileName);
				// 判断路径是否存在
				if (!filePath.getParentFile().exists()) {
					// 如果不存在，则递归创建此路径
					filePath.getParentFile().mkdirs();
				}
				System.out.println(uploadFileFileName);
				System.out.println(filePath.getParentFile());
				System.out.println(filePath);
				part.setImage(filePath.toString().substring(39)
						.replace('\\', '/'));
				// 将文件保存到硬盘上,Struts2会帮我们自动删除临时文件
				try {
					FileUtils.copyFile(uploadFile, filePath);
				} catch (IOException e) {
					System.out.println("图片上传失败");
					e.printStackTrace();
				}
			}
		} else {
			request.put("message", "请选择正确的图片!");
			return "addpart";
		}
		if (part.getName().isEmpty()) {
			request.put("message", "配件名称不能为空!");
		} else if (part.getPrice() == null) {
			request.put("message", "售价不能为空!");
		} else if (part.getStock() == null) {
			request.put("message", "库存不能为空!");
		} else if (partbo.addPart(part)) {
			request.put("message",
					"<script laguage='JavaScript'> alert('添加成功!') </script>");
		} else {
			request.put("message", "添加失败!");
		}
		return "addpart";
	}

	public String listPart() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Part> list;
		list = partbo.getAllPartByPage(pageNo, 15);
		totalRecords = partbo.getTotalPartRecords().intValue();
		PageBean<Part> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listPart_par");
		return "part";
	}

	public String listPartPrepare() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Part> list;
		list = partbo.getAllPartByPage(pageNo, 15);
		totalRecords = partbo.getTotalPartRecords().intValue();
		PageBean<Part> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listPartPrepare_par");
		return "listpart";
	}

	public String editPartPrepare() throws Exception {
		part = partbo.getPartById(Part.class, part.getId());
		request.put("part", part);
		return "editpart";
	}

	public String editPart() throws Exception {
		if (uploadFile == null) {
			request.put("message", "请选择正确的图片!");
			return "addpart";
		}
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"/images");
		SimpleDateFormat date = new SimpleDateFormat("/yyyy/MM/dd");
		String dateTime = date.format(new Date());
		realPath += dateTime;

		uploadFileFileName = UUID.randomUUID().toString()
				+ uploadFileFileName.substring(uploadFileFileName
						.lastIndexOf('.'));

		System.out.println(uploadFileContentType);
		// 控制图片类型
		if (uploadFileContentType.equals("image/gif")
				|| uploadFileContentType.equals("image/jpeg")
				|| uploadFileContentType.equals("image/png")
				|| uploadFileContentType.equals("image/bmp")
				|| uploadFileContentType.equals("image/x-icon")
				|| uploadFileContentType.equals("image/pjpeg")) {
			// 判断文件是否为空,并且文件不能大于2M
			if (uploadFile != null && uploadFile.length() < 2097152) {
				// 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
				File filePath = new File(new File(realPath), uploadFileFileName);
				// 判断路径是否存在
				if (!filePath.getParentFile().exists()) {
					// 如果不存在，则递归创建此路径
					filePath.getParentFile().mkdirs();
				}
				System.out.println(uploadFileFileName);
				System.out.println(filePath.getParentFile());
				System.out.println(filePath);
				part.setImage(filePath.toString());
				// 将文件保存到硬盘上,Struts2会帮我们自动删除临时文件
				try {
					FileUtils.copyFile(uploadFile, filePath);
				} catch (IOException e) {
					System.out.println("图片上传失败");
					e.printStackTrace();
				}
			}
		} else {
			request.put("message", "请选择正确的图片!");
			return "addpart";
		}
		if (part.getName().isEmpty()) {
			request.put("message", "地址不能为空!");
			return this.editPartPrepare();
		} else if (part.getPrice() == null) {
			request.put("message", "售价不能为空!");
			return this.editPartPrepare();
		} else if (part.getStock() == null) {
			request.put("message", "库存不能为空!");
			return this.editPartPrepare();
		}
		if (partbo.editPart(part)) {
			request.put("message",
					"<script laguage='JavaScript'> alert('修改成功!') </script>");
			return this.listPartPrepare();
		} else {
			request.put("message", "修改失败!");
		}
		return this.listPartPrepare();
	}

	public String removePart() throws Exception {
		partbo.removePart(part.getId());
		return this.listPartPrepare();
	}

}
